import router from './router';
import store from './store';
import { setTitle } from '@/utils/setTitle';
import NProgress from 'nprogress';
import 'nprogress/nprogress.css';
NProgress.configure({ showSpinner: false });

const whiteList = ['/', '/404', '/empty']; // no redirect whitelist

router.beforeEach((to, form, next) => {
  NProgress.start();
  if (to.meta.title) {
    document.title = to.meta.title;
    setTitle(to.meta.title);
    console.log('permission', document.title);
  }
  const needToken = store.getters.settings.needToken;

  if (needToken) {
    const hasToken = store.getters.token;
    if (hasToken) {
      next();
    } else if (whiteList.includes(to.path)) {
      next();
    } else {
      next({ path: '/' });
    }
  } else {
    next();
  }
});

router.afterEach(() => {
  NProgress.done();
});
